//
//  AlertModifier.swift
//  SwiftuiUno
//
//  Created by hccc on 2023/12/7.
//

import Foundation
import SwiftUI

struct AlertDismissViewModifier: ViewModifier {
    @Binding var isShow: Bool
    
    func body(content: Content) -> some View {
        ZStack {
            Color.black.opacity(0.3)
                .ignoresSafeArea()
                .allowsHitTesting(true)
                .onTapGesture {
                    withAnimation {
                        isShow = false
                    }
                }
                .zIndex(1)
            
            content
                .paddingHorizontal(16)
                .paddingVerticol(20)
                .background(Color.white)
                .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                .paddingHorizontal(20)
                .shadow(color:Color.black.opacity(0.2), radius: 6)
                .transition(.opacity)
                .zIndex(2)
        }
        .zIndex(1)
    }
}

struct AlertViewModifier: ViewModifier {
    
    func body(content: Content) -> some View {
        ZStack {
            Color.clear
                .ignoresSafeArea()
            
            content
                .paddingHorizontal(16)
                .paddingVerticol(20)
                .background(Color.white)
                .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                .paddingHorizontal(20)
                .shadow(color: .black.opacity(0.5), radius: 6)
                .transition(.asymmetric(
                    insertion: .scale.combined(with: .opacity),
                    removal: .move(edge: .bottom).combined(with: .opacity)
                )
                )
            
        }
    }
}

extension View {
    func makeAlert() -> some View {
        modifier(AlertViewModifier())
    }
    
    func makeDismissAlert(show: Binding<Bool>) -> some View {
        modifier(AlertDismissViewModifier(isShow: show))
    }
    
}
